package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.basics.service.config.Config;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.dto.Properties;
import com.sxbbc.common.core.entity.jz.CReceivedAddr;
import com.sxbbc.common.core.entity.jz.WWarehouse;
import com.sxbbc.common.core.entity.jz.WWarehouseDetail;
import com.sxbbc.common.core.mapper.jz.WWarehouseMapper;
import com.sxbbc.common.core.service.ICReceivedAddrService;
import com.sxbbc.common.core.service.IWWarehouseDetailService;
import com.sxbbc.common.core.service.IWWarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 仓库信息表 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-07-01
 */
@SuppressWarnings("all")
@Service
public class WWarehouseServiceImpl extends ServiceImpl<WWarehouseMapper, WWarehouse> implements IWWarehouseService {

    @Autowired
    private ICReceivedAddrService receivedAddrService;
    @Autowired
    private IWWarehouseDetailService warehouseDetailService;

    @Override
    public Integer selectWarehouseIdByAddr(Integer addrId, String proCode, Integer skuId,Integer qty) {
        //仓库ID
        Integer wid = null;
        CalcuUtils calUtil = CalcuUtils.getInstance();
        //获取到收货地址信息
        CReceivedAddr receivedAddr = receivedAddrService.getById(addrId);
        //拿到收货地址经纬度
        Properties properties = Config.properties;
        Map<String, String> map = calUtil.getLatAndLonByAmap(properties.getAmapKey(), receivedAddr.getNameExt() + receivedAddr.getAddress());
        String lat = map.get("lat");
        String lon = map.get("lon");
        //根据商品编码和商品详情拿到仓库的数据 qty大于所需库存 且 状态为1
        List<WWarehouse> list = super.baseMapper.queryWarehouseByProduct(proCode,skuId,qty);
        //初始距离
        double originalDistance = 0;
        if (!CollectionUtils.isEmpty(list)) {
            //初始距离取第一个比较
            if(originalDistance == 0){
                originalDistance = calUtil.getDistance(Double.parseDouble(list.get(0).getLatVal()),
                        Double.parseDouble(list.get(0).getLonVal()), Double.parseDouble(lat), Double.parseDouble(lon));
            }
            for (WWarehouse w : list) {
                if (calUtil.isEquals(w.getCityId(),receivedAddr.getCityId())) {
                    //比较市ID
                    wid = w.getId();
                }else if(calUtil.isEquals(w.getProId(),receivedAddr.getProId())){
                    //比较省ID
                    wid = w.getId();
                }else{
                    //比较经纬度
                    double distance = calUtil.getDistance(Double.parseDouble(w.getLatVal()),
                            Double.parseDouble(w.getLonVal()), Double.parseDouble(lat), Double.parseDouble(lon));
                    //计算的距离比前一个距离小
                    if (distance <= originalDistance) {
                        originalDistance = distance;
                        wid = w.getId();
                    }
                }
            }
        }else{
            //没有符合的仓库条件
            return null;
        }
        //仓库ID不为空时扣减相应库存
        if (wid != null) {
            QueryWrapper<WWarehouseDetail> wrapper = new QueryWrapper<>();
            wrapper.eq("wid",wid).eq("pro_code",proCode).eq("sku_id",skuId);
            WWarehouseDetail wWarehouseDetail = warehouseDetailService.getOne(wrapper);
            if (wWarehouseDetail.getQty() >= qty) {
                //扣减后的库存
                Integer leftqty = wWarehouseDetail.getQty() - qty;
                wWarehouseDetail.setQty(leftqty);
                warehouseDetailService.updateById(wWarehouseDetail);
            }
        }
        return wid;
    }
}
